package com.orion.config;


import com.orion.domain.Result;
import com.orion.enumeration.CommonCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.time.format.DateTimeParseException;

/**
 * 全局异常处理
 *
 * controller的异常oauth2是不会捕捉的
 *
 * @author li.lc
 * @date 2019-09-16
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(DateTimeParseException.class)
    public Result actionDtpeExceptionHandle(DateTimeParseException dtpe
            , HttpServletRequest request) {
        log.warn("发生DateTimeParseException异常（{}） :", request.getRequestURI(), dtpe);
        return CommonCodeEnum.COMMON_INVALID_PARAM.toResult();
    }


    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result missingRequestParameterHandle(MissingServletRequestParameterException missingServletRequestParameterException,
                                                   HttpServletRequest request) {
        log.warn("发生MissingServletRequestParameterException异常（{}） :", request.getRequestURI(), missingServletRequestParameterException);
        return CommonCodeEnum.COMMON_MISSING_PARAM.toResult();
    }

    @ExceptionHandler(BindException.class)
    public Result actionBindExceptionHandle(BindException bindException
            , HttpServletRequest request) {
        log.warn("发生BindException异常（{}） :", request.getRequestURI(), bindException);
        return CommonCodeEnum.COMMON_INVALID_PARAM.toResult();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidExceptionHandle(MethodArgumentNotValidException methodArgumentNotValidException
            , HttpServletRequest request) {
        log.warn("发生MethodArgumentNotValidException异常（{}） :", request.getRequestURI(), methodArgumentNotValidException);
        return CommonCodeEnum.COMMON_INVALID_PARAM.toResult();
    }



}
